﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;

namespace YTSK.Frm
{
    public class FrmSKFPListBase : Report.ReportData
    {
        private ToolStrip _toolstrip;
        #region 初始化

        public FrmSKFPListBase(Control main, DataGridView dg, string ReportCode, ToolStrip toolstrip) :
            base(main, dg, ReportCode)
        { _toolstrip = toolstrip; }
        #endregion

        public override void InitControl()
        {
            base.InitControl();
            foreach (ToolStripItem bar in _toolstrip.Items)
            {
                if (bar.Text.Length > 0)
                { bar.Click += toolbar_click; }
            }
        }

        #region 双击选中行
        public void dataGridView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            try
            {
                var row = _dg.CurrentRow;
                if (row == null) { return; }
                var obj = row.Cells["ID"].Value.ObjToInt();               
                if (obj != 0)
                {
                    if (ServerConfig._company ==  Company.上海Iteng )
                    {
                        var f = new FrmKP.FrmSK();
                        f.Show();
                        f.Reflesh(obj);
                    }
                    else
                    {
                        var f = new FrmKPFPDisplay();
                        f.Show();
                        f.voudefine.Reflesh(obj);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion


        #region 工具栏事件
        public virtual void toolbar_click(object sender, EventArgs e)
        {
            if (sender.GetType() != typeof(ToolStripButton)) { return; }
            var bt = (ToolStripButton)sender;
            var text = bt.Text;
            switch (text)
            {
                case "导出":
                    Common.handleDataGrid.ExportExcel(base._dg);
                    break;
                case "作废发票":
                    AbandonVouch();
                    break;
                case "恢复作废发票":
                    CancelAbandonVouch();
                    break;
                case "红冲发票":
                    RedVouch();
                    break;
                case "修正发票号码":
                    ModifyVouchCode();
                    break;
                case "退出":
                    var parent = (Form)_main.Parent;
                    parent.Close();
                    break;
                default:
                    MessageBox.Show("没有找到程序");
                    break;
            }

        }
        #endregion

        #region 作废发票
        public virtual void AbandonVouch()
        {
            try
            {
                var currentrow = _dg.CurrentRow;
                if (currentrow == null) { return; }
                var _id = currentrow.Cells["ID"].Value.ObjToInt();
                var fphm = currentrow.Cells["fphm"].Value.ObjToString();

                var dr = MessageBox.Show(string.Format("确认要作废发票[{0}]么？请先在税控机上作废", fphm),"提示",MessageBoxButtons.YesNo);
                if (dr == DialogResult.No) { return; }
                var url = string.Format("{0}/api/oms/SKSalebillVouch/AbandonVouch?token={1}",
                        ServerConfig.ServerAddress, ServerConfig.GetToken());
                var pos = new Dictionary<string, object>();
                pos.Add("ID", _id);
                var s = Common.handleHttp.Post(pos, url);
                var result = Common.handleJSON.DeserializeResult(s);
                if (result.result == -1 || result.result == 1002)
                {
                    MessageBox.Show(result.resultdetail); return;
                }

                if (result.result != 1) { MessageBox.Show("作废发票出错:" + result.resultdetail); return; }
                MessageBox.Show("作废发票成功!");
                #region 显示数据
                base.GetGridData();
                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace);
            }
        }
        #endregion

        #region 恢复作废发票
        public virtual void CancelAbandonVouch()
        {
            try
            {
                var currentrow = _dg.CurrentRow;
                if (currentrow == null) { return; }
                var _id = currentrow.Cells["ID"].Value.ObjToInt();
                var fphm = currentrow.Cells["fphm"].Value.ObjToString();

                var dr = MessageBox.Show(string.Format("确认要恢复作废发票[{0}]么？", fphm),"提示", MessageBoxButtons.YesNo);
                if (dr == DialogResult.No) { return; }
                var url = string.Format("{0}/api/oms/SKSalebillVouch/CancelAbandonVouch?token={1}",
                        ServerConfig.ServerAddress, ServerConfig.GetToken());
                var pos = new Dictionary<string, object>();
                pos.Add("ID", _id);
                var s = Common.handleHttp.Post(pos, url);
                var result = Common.handleJSON.DeserializeResult(s);
                if (result.result == -1 || result.result == 1002)
                {
                    MessageBox.Show(result.resultdetail); return;
                }

                if (result.result != 1) { MessageBox.Show("恢复作废发票出错:" + result.resultdetail); return; }
                MessageBox.Show("恢复发票成功!");
                #region 显示数据
                base.GetGridData();
                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace);
            }
        }
        #endregion

        #region 红冲发票
        public virtual void RedVouch()
        {
            try
            {
                var currentrow = _dg.CurrentRow;
                if (currentrow == null) { return; }
                var _id = currentrow.Cells["ID"].Value.ObjToInt();
                var fphm = currentrow.Cells["fphm"].Value.ObjToString();

                var dr = MessageBox.Show(string.Format("确认要红冲发票[{0}]么？请先在税控机上红冲，然后输入新的发票", fphm),"提示", MessageBoxButtons.YesNo);
                if (dr == DialogResult.No) { return; }
                var ddate = DateTime.Now;
                var f = new FrmKP.FrmReturn_SK_Red(_id);
                f.ShowDialog();
                #region 显示数据
                base.GetGridData();
                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace);
            }
        }
        #endregion

        #region 修正发票号码
        public virtual void ModifyVouchCode()
        {
            try
            {
                var currentrow = _dg.CurrentRow;
                if (currentrow == null) { return; }
                var _id = currentrow.Cells["ID"].Value.ObjToInt();
                var fphm = currentrow.Cells["fphm"].Value.ObjToString();
                var fpdm = currentrow.Cells["fpdm"].Value.ObjToString();
                var ddate = currentrow.Cells["dDate"].Value.ObjToDate();
                var f = new FrmKP.FrmReturn_SK_FPHM(_id, fpdm, fphm, ddate);
                f.ShowDialog();
                #region 显示数据
                base.GetGridData();
                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace);
            }
        }
        #endregion


    }
}
